// pages/form/form_table.js
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    bio: '',
    username: "",
    mobile: "",
  },
  onShareAppMessage: function (res) {
    return {
      title: '赏金达人',
      path: '/pages/form/form_table/form_table',
      imageUrl: 'https://zs.cdbbox.com/uploads/images/20200323194452.jpg',
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getRegister()
  },
  getRegister: function () {
    var unionId = app.globalData.userInfo.unionId;
    //console.log(app.globalData.userInfo)
    var that = this
    //请求排行榜
    wx.request({
      url: 'https://zs.cdbbox.com/index/api/get_register',
      data: {
        unionid: unionId
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      method: 'POST',
      success: function (res) {
        //console.log(res.data)
        that.setData({
          mobile: res.data.mobile,
          username: res.data.username,
          bio: res.data.bio,
        })
      }
    })
  },
  onusername: function (e) {
    var val = e.detail.value;
    this.setData({
       username: val
    });
  },   

  onmobile: function (e) {
    var val = e.detail.value;
    this.setData({
      mobile: val
    });
  },  

  onbio: function (e) {
    var val = e.detail.value;
    this.setData({
      bio: val
    });
  },  

  formSubmit:function(){
    var bio=  this.data.bio
    var username = this.data.username
    var mobile = this.data.mobile
    if (username == '') {
        wx.showToast({
          title: '请输入用户名',
          icon: 'succes',
          duration: 1000,
          mask: true
        })

        return false
      }

    if (mobile.length != 11) {
      wx.showToast({
        title: '手机号长度有误！',
        icon: 'success',
        duration: 1500
      })
      return false;
    }
    var unionId = app.globalData.userInfo.unionId
    //console.log(unionId)
    //var unionId = wx.getStorageSync('unionId');
    wx.request({
      url: 'https://zs.cdbbox.com/index/api/register_u',
      data: {
        unionid: unionId,
        mobile: mobile,
        username: username,
        bio: bio,
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      method: 'POST',
      success: function (res) {
        if(res.data){
          wx.showToast({
            title: '报名成功！',
            icon: 'success',
            duration: 3000
          })
        }else{
          wx.showToast({
            title: '报名失败！',
            icon: 'success',
            duration: 3000
          })
        }
        setTimeout(function () {
          wx.redirectTo({
            url: "/pages/index/result/result"
          })
        }, 3000)
  
        

      }
    })


  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
  
})